package com.xc.hotelmgr.webapi.modules.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xc.hotelmgr.webapi.common.utils.HttpClient;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.Query;
import com.xc.hotelmgr.webapi.modules.dt.entity.StoreEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.StoreService;
import com.xc.hotelmgr.webapi.modules.sys.dao.SysMqDao;
import com.xc.hotelmgr.webapi.modules.sys.entity.SysMqConsumerEntity;
import com.xc.hotelmgr.webapi.modules.sys.entity.SysMqEntity;
import com.xc.hotelmgr.webapi.modules.sys.service.MqInstance;
import com.xc.hotelmgr.webapi.modules.sys.service.SysMqService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("sysMqService")
public class SysMqServiceImpl extends ServiceImpl<SysMqDao, SysMqEntity> implements SysMqService {

    private static final String CONSUMER_SERVICE_NAME = "consumer-mq";

    @Autowired
    private SysMqService sysMqService;

    @Autowired
    private MqInstance mqInstance;

    @Autowired
    private StoreService storeService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String dtName = (String)params.get("dtName");
        String storeName = (String) params.get("storeName");

        IPage<SysMqEntity> page = this.page(
                new Query<SysMqEntity>().getPage(params),
                new QueryWrapper<SysMqEntity>()
                        .like(StringUtils.isNotBlank(dtName),"dt_name", dtName)
                        .like(StringUtils.isNotBlank(storeName), "store_name", storeName)
        );

        return new PageUtils(page);
    }

    /**
     * 容器添加消费者
     * @param map
     * @return
     */
    @Override
    @Transactional
    public boolean mqAdd(Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        SysMqEntity sysMqEntity = sysMqService.getById(id);
        Integer storeId = Math.toIntExact(sysMqEntity.getStoreId());
        StoreEntity storeEntity = storeService.getById(storeId);
        String appKey = storeEntity.getAppkey();
        List<URI> uris = mqInstance.getInstance(CONSUMER_SERVICE_NAME);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("appKey", appKey);
        for (URI uri:uris) {
            String url = uri.toString() + "/consumer/add";
            String jsonObject = HttpClient.doPost(map1, url);
            JSONObject jsonObject1 = JSONObject.parseObject(jsonObject);
            Integer code = jsonObject1.getInteger("code");
            if(code!=0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean mqUpdate(SysMqConsumerEntity sysMqConsumerEntity) {
        List<URI> uris = mqInstance.getInstance(CONSUMER_SERVICE_NAME);
        Map<String, Object> map = new HashMap<>();
        int size = uris.size();
        map.put("sendAll", (int)sysMqConsumerEntity.getSendAll()/size);
        map.put("upDown", (int)sysMqConsumerEntity.getHtUpDown()/size);
        map.put("htCallback", (int)sysMqConsumerEntity.getCallbackHotel()/size);
        map.put("rpCallback", (int)sysMqConsumerEntity.getCallbackRp()/size);
        map.put("ratesCallback", (int)sysMqConsumerEntity.getCallbackRates()/size);
        map.put("MQ2MQHotel", (int)sysMqConsumerEntity.getChangeHotel()/size);
        map.put("MQ2MQRp", (int)sysMqConsumerEntity.getChangeRp()/size);
        map.put("MQ2MQRates", (int)sysMqConsumerEntity.getChangeRates()/size);
        map.put("delete_rp", (int)sysMqConsumerEntity.getDeleteRp()/size);
        map.put("hotel_id_list", (int)sysMqConsumerEntity.getHotelIdList()/size);
        map.put("hotel_id_list_all", (int)sysMqConsumerEntity.getHotelIdListAll()/size);
        map.put("xc_change_rates", (int)sysMqConsumerEntity.getXcChangeRates()/size);
        for (URI uri:uris) {
            String url = uri.toString() + "/consumer/update";
            String jsonObject = HttpClient.doPost(map, url);
            JSONObject jsonObject1 = JSONObject.parseObject(jsonObject);
            Integer code = jsonObject1.getInteger("code");
            if(code!=0) {
                return false;
            }
        }
        return true;
    }

}